﻿//-----------------------------------------------------------------------------------------------
// <copyright file="TraceObject.cs" company="VSSD" author="VSSD-KT" initializationUser="TriDM">
//     Copyright © VietNam Securities Software Development (VSSD) 2009.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------------------------

namespace Lapas.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Lớp định nghĩa đối tượng Trace (theo dõi sự kiện và trace log)
    /// </summary>
    public class TraceObject
    {
        /// <summary>
        /// Định nghĩa hàm xử lý sự kiện Raise message
        /// </summary>
        /// <param name="traceObject">đối tượng trace</param>
        public delegate void TraceMessageRaisedEventHandler(TraceObject traceObject);

        /// <summary>
        /// Định nghĩa sự kiện MessageChange
        /// </summary>
        public event TraceMessageRaisedEventHandler TraceMessageRaised;

        /// <summary>
        /// Gets or sets a value indicating whether Enable Trace - Cho phép thực hiện Trace hay không?
        /// </summary>
        public static bool EnableTrace { get; set; }

        /// <summary>
        /// Gets or sets messsage
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets source object information
        /// </summary>
        public string SourceObjectInformation { get; set; }

        /// <summary>
        /// Gets or sets TraceTime
        /// </summary>
        public DateTime TraceTime { get; set; }

        /// <summary>
        /// Export thông tin đối tượng vào XmlTextWriter
        /// </summary>
        /// <param name="xmlTextWriter">đối tượng XmlTextWriter</param>
        public void WriteToXml(XmlTextWriter xmlTextWriter)
        {
            xmlTextWriter.WriteStartElement("TraceObject");
            List<PropertyInfo> props = Helper.GetTargetProperties(this.GetType());
            foreach (PropertyInfo propInfor in props)
            {
                string propName = propInfor.Name;
                object value = propInfor.GetValue(this, null);
                if (value != null)
                {
                    if (value.ToString().Trim().Length > 0)
                    {
                        xmlTextWriter.WriteElementString(propName, value.ToString().Trim());
                    }
                    else
                    {
                        xmlTextWriter.WriteElementString(propName, string.Empty);
                    }
                }
                else
                {
                    xmlTextWriter.WriteElementString(propName, string.Empty);
                }
            }
            
            xmlTextWriter.WriteEndElement();
        }

        /// <summary>
        /// Hàm tạo sự kiện MessageChange
        /// </summary>
        /// <param name="message">message - thông báo</param>
        public void RaiseTraceMessage(string message)
        {
            this.RaiseTraceMessage(null, message);
        }

        /// <summary>
        /// Hàm tạo sự kiện MessageChange
        /// </summary>
        /// <param name="source">đối tượng gốc phát sinh sự kiện</param>
        /// <param name="message">message - thông báo</param>        
        public void RaiseTraceMessage(object source, string message)
        {
            if (source != null)
            {
                this.RaiseTraceMessage(source.GetType().FullName, message);
            }
            
            ////if (EnableTrace)
            ////{
            ////    if (this.TraceMessageRaised != null)
            ////    {
            ////        if (source == null)
            ////        {
            ////            this.TraceMessageRaised(new TraceObject { Message = message, SourceObjectInformation = string.Empty, TraceTime = DateTime.Now });
            ////        }
            ////        else
            ////        {
            ////            this.TraceMessageRaised(new TraceObject { Message = message, SourceObjectInformation = source.GetType().FullName, TraceTime = DateTime.Now });
            ////        }
            ////    }
            ////}
        }

        /// <summary>
        /// Hàm tạo sự kiện MessageChange
        /// </summary>
        /// <param name="sourceInformation">Thông tin về đối tượng source</param>
        /// <param name="message">message - thông báo</param>        
        public void RaiseTraceMessage(string sourceInformation, string message)
        {
            if (EnableTrace)
            {
                if (this.TraceMessageRaised != null)
                {
                    this.TraceMessageRaised(new TraceObject { Message = message, SourceObjectInformation = sourceInformation, TraceTime = DateTime.Now });
                }
            }
        }
    }
}
